{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-06T02:30:26.142097Z",
     "start_time": "2025-06-06T02:30:24.644915Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "from collections import Counter\n",
    "\n",
    "# KNN 分类器类\n",
    "class KNNClassifier:\n",
    "    def __init__(self, k=3):\n",
    "        self.k = k\n",
    "\n",
    "    def fit(self, X_train, y_train):\n",
    "        \"\"\"保存训练数据\"\"\"\n",
    "        self.X_train = X_train\n",
    "        self.y_train = y_train\n",
    "\n",
    "    def predict(self, X_test):\n",
    "        \"\"\"对测试集进行预测\"\"\"\n",
    "        predictions = [self._predict_single(x) for x in X_test]\n",
    "        return np.array(predictions)\n",
    "\n",
    "    def _predict_single(self, x):\n",
    "        \"\"\"预测单个样本\"\"\"\n",
    "        # 1. 计算当前样本与所有训练样本的距离\n",
    "        distances = [np.linalg.norm(x - x_train) for x_train in self.X_train]\n",
    "\n",
    "        # 2. 找到距离最近的k个样本的索引\n",
    "        k_indices = np.argsort(distances)[:self.k]\n",
    "\n",
    "        # 3. 获取这k个样本的标签\n",
    "        k_nearest_labels = [self.y_train[i] for i in k_indices]\n",
    "\n",
    "        # 4. 投票：统计最多出现的标签\n",
    "        most_common = Counter(k_nearest_labels).most_common(1)\n",
    "        return most_common[0][0]\n",
    "\n",
    "# 示例用法：\n",
    "if __name__ == \"__main__\":\n",
    "    from sklearn.datasets import make_classification\n",
    "    from sklearn.model_selection import train_test_split\n",
    "    from sklearn.metrics import accuracy_score\n",
    "\n",
    "    # 创建一个简单的二分类数据集\n",
    "    X, y = make_classification(n_samples=300, n_features=4, random_state=42)\n",
    "    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "    # 初始化模型并训练\n",
    "    knn = KNNClassifier(k=5)\n",
    "    knn.fit(X_train, y_train)\n",
    "\n",
    "    # 预测和评估\n",
    "    y_pred = knn.predict(X_test)\n",
    "    print(\"准确率：\", accuracy_score(y_test, y_pred))"
   ],
   "id": "340426d2b19ce95",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率： 0.9333333333333333\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "d15d797694dffc5b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
